# 测试功能：如何删除字符串中指定字符
print("hello world")
a = '你是谁'
a = a.replace('你是', '')
print(a)

# 测试功能：如何获取元素中的文字
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

driver = webdriver.Chrome(executable_path='D:/chromedriver.exe')
driver.set_window_size(1280,720)

vid = input('请输入av/bv号：')
url = 'https://www.bilibili.com/video/' + vid
driver.get(url)
sleep(10)

# 获取标题数据
title1 = driver.find_element(By.XPATH,'//*[@id="viewbox_report"]/h1').text
print('获取标题数据', title1)

# 获取播放量数据
play1 = driver.find_element(By.XPATH,'//*[@id="viewbox_report"]/div/span[1]').get_attribute('title')
play1 = play1.replace('总播放数', '')
print('获取播放量', play1)

# 获取点赞数
like1 = driver.find_element(By.XPATH,'//*[@id="arc_toolbar_report"]/div[1]/span[1]').text
print('获取点赞数', like1)

# 获取投币数
coin1 = driver.find_element(By.XPATH,'//*[@id="arc_toolbar_report"]/div[1]/span[2]').text
print('获取投币数', coin1)

# 获取收藏数
collect1 = driver.find_element(By.XPATH,'//*[@id="arc_toolbar_report"]/div[1]/span[3]').text
print('获取收藏数', collect1)

# 获取分享数
share1 = driver.find_element(By.XPATH,'//*[@id="arc_toolbar_report"]/div[1]/span[4]').text
print('获取分享数', share1)

# 获取弹幕数
damuku1 = driver.find_element(By.XPATH, '//*[@id="viewbox_report"]/div/span[2]').text
damuku1 = damuku1.replace('总弹幕数', '')
print('获取弹幕数', damuku1)

# 获取评论数
sleep(5)
comment1 = driver.find_element(By.XPATH, '//*[@id="comment"]/div/div[1]/span[1]').text
print('获取评论数', comment1)

# 写入csv文件
# coding=UTF-8
filename = 'text.csv'
with open(filename, 'a') as file_object:
    file_object.write('标题,播放量,点赞,硬币,收藏,分享,弹幕,评论\n')
    file_object.write(title1 + ',' + play1 + ',' + like1 + ',' + coin1 + ',' + collect1 + ',' + share1 + ',' + damuku1 + ',' + comment1 + '\n')

sleep(10)
driver.quit()
